package com.d.ding.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.d.business.domain.dto.DingProcessConfDTO;
import com.d.business.domain.dto.DingProcessConfItemDTO;
import com.d.client.dto.DingProcessInstanceWirteDTO;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Service
public class DingBaseImpl {

    private Object getObject(Object o, String className, String fieldName) {
        try {
            Class<?> clazz = Class.forName(className);
            // 获取id字段
            Field idField = clazz.getDeclaredField(fieldName);
            idField.setAccessible(true);
            return idField.get(o);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    //组装
    public DingProcessInstanceWirteDTO dingFrom(Object o, DingProcessConfDTO conf) {
        DingProcessInstanceWirteDTO dto = new DingProcessInstanceWirteDTO();
        dto.setProcessCode(conf.getProcessCode());
        dto.setOriginatorUserId(this.getObject(o,conf.getQueryBeanClass(),"dingUserId").toString());
        dto.setDeptId(this.getObject(o,conf.getQueryBeanClass(),"dingDeptId").toString());
//            dto.setMicroappAgentId(3010740226L+"");


        //组装ding数据结构
        List<DingProcessInstanceWirteDTO.FormComponentValuesBean> formComponentValues = new ArrayList<>();
        for (DingProcessConfItemDTO formK : conf.getDingProcessConfItemList()) {
            DingProcessInstanceWirteDTO.FormComponentValuesBean bean = new DingProcessInstanceWirteDTO.FormComponentValuesBean();
            bean.setName(formK.getDingField());
            bean.setValue(dynamycDataQuery(formK, o).toString());
            formComponentValues.add(bean);
        }
        dto.setFormComponentValues(formComponentValues);
        return dto;

    }


    /**
     * 动态处理数据
     *
     * @param formK
     * @param bean
     * @return
     */
    private Object dynamycDataQuery(DingProcessConfItemDTO formK, Object bean) {
        Integer type = formK.getDingDataType();
        String valueJoinFieldName = formK.getValueJoinField();
        List<DingProcessConfItemDTO> child = formK.getChild();
        try {
            //1. 获取Class对象
            Class<?> clazz = bean.getClass();
            // 2. 获取字段对象???
            Field field = clazz.getDeclaredField(valueJoinFieldName);
            // 3. 设置可访问性
            field.setAccessible(true);
            if (type == 3) {
                // 4. 获取字段值
                Object obj = field.get(bean);

                JSONObject json = new JSONObject();
                for (DingProcessConfItemDTO itemConstants : child) {
                    Object r = this.dynamycDataQuery(itemConstants, obj);
                    json.put(itemConstants.getDingField(), r);
                }
            } else if (type == 4) {//一维数组
                String result = "";
                if (field.get(bean) == null)
                    return null;
                String fStr = field.get(bean).toString();
                if (fStr.indexOf("[") != -1) {
                    result += field.get(bean).toString();
                } else {
                    //仅针对逗号分隔情况
                    String[] strList = fStr.split(",");
                    JSONArray jsonArray = new JSONArray();
                    for (String s : strList) {
                        jsonArray.add(s);
                    }
                    return jsonArray;
                }
                return result;
            } else if (type == 5 || type == 6) {//多维数组

                JSONArray jsonArray = new JSONArray();
                // 4. 获取list值
                List<?> list = (List<?>) field.get(bean);
                for (Object item : list) {

                    JSONArray jsonArray2 = new JSONArray();
                    for (DingProcessConfItemDTO itemConstants : child) {
                        Object r = this.dynamycDataQuery(itemConstants, item);
                        JSONObject json = new JSONObject();
                        json.put("name", itemConstants.getValueJoinField());
                        json.put("value", r);
                        jsonArray2.add(json);
                    }
                    jsonArray.add(jsonArray2);
                }
                return jsonArray;
            } else {
                if (field.get(bean) == null)
                    return null;
                return field.get(bean).toString();
            }
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
